package cn.rkylin.oms.system.monthlyTurnoverReport.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.base.brand.controller.BrandController;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.system.monthlyTurnoverReport.service.IMonthlyTurnoverReportService;
import cn.rkylin.oms.system.monthlyTurnoverReport.vo.MonthlyTurnoverReportVO;

/**
 * 月营业额报表
 * 
 * @ClassName MonthlyTurnoverReportController.java
 * @author 崔继扬
 * @version V1.0 2020-04-24
 */
@Controller
@RequestMapping("/monthlyTurnoverReport")
public class MonthlyTurnoverReportController extends ApolloController{
	
    private static final Log logger = LogFactory.getLog(BrandController.class);

    @Autowired
    private IMonthlyTurnoverReportService monthlyTurnoverReportServie;
    
    @ResponseBody
    @RequestMapping(value = "/getMonthlyTurnoverReport")
    public Map<String, Object> getMonthlyTurnoverReport(String formJson, @RequestParam(required = false, defaultValue = "0") int start,
            @RequestParam(required = false, defaultValue = "10") int length) throws Exception {
        logger.info("查询月营业额报表：/MonthlyTurnoverReport/getMonthlyTurnoverReport");

        MonthlyTurnoverReportVO monthlyTurnoverReportVO = new MonthlyTurnoverReportVO();
        Map<String,String> mapMonthlyTurnoverReport = JSONObject.parseObject(formJson, Map.class);
        
        if (mapMonthlyTurnoverReport != null) {
            if (StringUtils.isNotEmpty(mapMonthlyTurnoverReport.get("shop_id"))) {
            	monthlyTurnoverReportVO.setShopId(mapMonthlyTurnoverReport.get("shop_id"));
            }
            if (StringUtils.isNotEmpty(mapMonthlyTurnoverReport.get("pay_year"))) {
            	monthlyTurnoverReportVO.setPayYear(mapMonthlyTurnoverReport.get("pay_year"));
            }
            if (StringUtils.isNotEmpty(mapMonthlyTurnoverReport.get("pay_month"))) {
            	monthlyTurnoverReportVO.setPayMonth(mapMonthlyTurnoverReport.get("pay_month"));
            }
        }
        
        // 下面代码放到private方法中时，会获取到上一个请求中的currentUser，所以先移到这里。
        Map<String, Object> returnPrjIdMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
        	returnPrjIdMap.put(JSON_RESULT, FAILED);
        	returnPrjIdMap.put(JSON_MSG, "当前用户失效");
            return returnPrjIdMap;
        }
        String prjId = currentUser.getCurrentProject().getPrjId();
        monthlyTurnoverReportVO.setPrjId(prjId);
        
        // 处理分页信息
        if (length == -1) {
            length = Integer.MAX_VALUE;
        }
        int page = start / length + 1;
        
        // 执行查询
        PageInfo<MonthlyTurnoverReportVO> brandVOList = monthlyTurnoverReportServie.getMonthlyTurnoverReport(page, length, monthlyTurnoverReportVO);
        
        Map<String, Object> returnMap = new HashMap<>();
        if (null != brandVOList) {
            returnMap = getSuccessMap("查询月营业额报表数据成功");
            returnMap.put(RECORDS_FILTERED, brandVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, brandVOList.getTotal());
            returnMap.put(RETURN_DATA, brandVOList.getList());
        } else {
            returnMap = getFailedMap("查询月营业额报表数据失败");
        }
        logger.info("查询月营业额报表数据调用结束");
        return returnMap;
    }
    
    private Map<String, Object> getSuccessMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, SUCCESS);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    }

    private Map<String, Object> getFailedMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, FAILED);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    }
}
